package com.job.model.admin;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.ForeignKey;
import org.hibernate.annotations.NamedQueries;
import org.hibernate.annotations.NamedQuery;
@Entity
@Table(name="job_resource")
@NamedQueries({
	@NamedQuery(name = "menuList", query = "from Resource r where r.parentResource is null"),
	@NamedQuery(name = "menuListCount", query = "select count(r.id) from Resource r where r.parentResource is null")
})
public class Resource implements Serializable {
	private static final long serialVersionUID = 1L;
	private int id;
	private String name;
	private String path;
	private Set<Group> groups;
	private Set<Resource> resources;
	private Resource parentResource;
	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}
	@ManyToMany
	@ForeignKey(name="resource_group_fk",inverseName="group_resource_fk")
	@JoinTable(name="job_privilege",joinColumns={@JoinColumn(name="resourceId")},inverseJoinColumns={@JoinColumn(name="groupId")})
	public Set<Group> getGroups() {
		return groups;
	}
	public void setGroups(Set<Group> groups) {
		this.groups = groups;
	}
	@OneToMany(mappedBy="parentResource")
	public Set<Resource> getResources() {
		return resources;
	}
	public void setResources(Set<Resource> resources) {
		this.resources = resources;
	}
	@ManyToOne
	@JoinColumn(name="parentId")
	@ForeignKey(name="resource_fk")
	public Resource getParentResource() {
		return parentResource;
	}
	public void setParentResource(Resource parentResource) {
		this.parentResource = parentResource;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Resource other = (Resource) obj;
		if (id != other.id)
			return false;
		return true;
	}
	
}
